CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000


  
'set the desired I2C pins here.
'SDA     = 1
'SCL     = 0


'vvvvv Use these if you want to talk to the Propeller's EEPROM
SDA     = 29
SCL     = 28


EE_DEVICE_ADDR  = $A0



obj

  i2c   :       "Basic_I2C_Driver"  
  d     :       "Parallax Serial Terminal"
  
pub go | i, in

  'i2c.StartI2C(SDA, SCL)
  'i2c.StartI2C(PROPELLER_SDA, PROPELLER_SCL)

  d.start(115_200)

  PauseMS(5000)
  d.clear

  'WriteJunk
  
  PrintRaw
  'PrintChartStyle

pub WriteJunk | i

  d.str(string("Writing bytes..."))
  repeat i from $0000 to $1000

    WriteByte(i, i&$FF)
    d.str(string("Writing "))
    d.hex(i&$FF, 2)
    d.str(string(" at 0x"))
    d.hex(i, 8)
    d.char(13)
    
  
pub WriteByte(addr, in)

  i2c.WriteByte(SCL, EE_DEVICE_ADDR, addr, in)
  i2c.WriteWait(SCL, EE_DEVICE_ADDR, addr)

pub PrintRaw | i

  repeat i from $0000 to $8000-1
  
    d.char(i2c.ReadByte(SCL, EE_DEVICE_ADDR, i))    '<----- transmits the raw value to the terminal

    'd.char(" ")                'add space
    
    'd.char(13)                 'add newline

pub PrintChartStyle | i

  d.str(string("Dumping EEPROM Contents:", 13))

  i~
  repeat ($8000/16)             '16 cols in a line
    
    d.str(string("0x"))
    d.hex(i, 8)
    d.char(":")
    d.char(" ")

    repeat 16
      d.hex(i2c.ReadByte(SCL, EE_DEVICE_ADDR, i++), 2)
      d.char(" ")

    d.char(13)
    
    
    'd.hex(i2c.readByte(i), 2)   '<----- Prints the ASCII equivalent of the hex value in the EEPROM.



  d.str(string("============= Done =============="))
    
pub PauseMS(Duration)
''  Pause execution for 'duration' milliseconds.
''  This routine is based on the set clock frequency.
''  
''  params:  Duration = number of milliseconds to delay                                                                                               
''  return:  none

  waitcnt((((clkfreq / 1_000) * Duration - 3932) #> 381) + cnt)  
    